import 'package:flutter/material.dart';
import 'package:flutter_company_app/model/product.dart';
import 'package:flutter_company_app/style/index.dart';

import 'product_detail_page.dart';

class ProductListPage extends StatelessWidget {
  final ProductListModel list;
  final Function getNextPage;
  const ProductListPage({Key key, this.list, this.getNextPage})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (list.data.length == 0) {
      return Center(child: CircularProgressIndicator());
    }
    return ListView.builder(
        padding: EdgeInsets.symmetric(horizontal: 10),
        itemCount: list.data.length,
        itemBuilder: (BuildContext context, int index) {
          ProductItemModel product = list.data[index];
          // 加载下一页
          if ((index + 4) == list.data.length) {
            getNextPage();
          }
          return GestureDetector(
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => ProductDetailPage(item: product),
                ),
              );
            },
            child: Container(
              color: ProductColors.bgColor,
              padding: EdgeInsets.only(top: 10, right: 5),
              child: Row(
                // 产品照片
                children: [
                  Image.network(product.imageUrl,
                      width: 120, height: 120, fit: BoxFit.cover),
                  SizedBox(width: 10),
                  Expanded(
                    child: Container(
                      decoration: BoxDecoration(
                        border: Border(
                          bottom: BorderSide(
                            width: 1,
                            color: ProductColors.divideLineColor,
                          ),
                        ),
                      ),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(product.desc),
                          SizedBox(height: 5),
                          Row(
                            children: [
                              SizedBox(width: 5),
                              Text(
                                product.type,
                                style: TextStyle(
                                  fontSize: 16,
                                  color: ProductColors.typeColor,
                                ),
                              ),
                              SizedBox(width: 8),
                              product.point == null
                                  ? SizedBox()
                                  : Container(
                                      child: Text(
                                        product.point,
                                        style: TextStyle(
                                          color: ProductColors.pointColor,
                                        ),
                                      ),
                                      padding:
                                          EdgeInsets.symmetric(horizontal: 3),
                                      margin: EdgeInsets.only(left: 4),
                                      decoration: BoxDecoration(
                                        border: Border.all(
                                            width: 1,
                                            color: ProductColors.pointColor),
                                      ),
                                    ),
                            ],
                          ),
                          SizedBox(height: 5),
                          Text(product.name, style: ProductFonts.itemNameStyle)
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            ),
          );
        });
  }
}
